﻿using System.Web.Mvc.Html;
using SlamCms.Common;
using SlamCms.Configuration;
using SlamCms.Data;
using SlamCms.Web;
using SlamCms.Web.Mvc;
using SlamCms.Web.Sitemaps;
using StackExchange.Profiling;

namespace System.Web.Mvc
{
	public class SlamCmsHtmlHelper
	{
		private readonly HtmlHelper _htmlHelper;
		private readonly Lazy<SlamCmsContext> _slamCmsContext = new Lazy<SlamCmsContext>(() => DependencyResolver.Current.GetService<SlamCmsContext>()); 

		public SlamCmsHtmlHelper(HtmlHelper htmlHelper)
		{
			_htmlHelper = htmlHelper;
		}

		public HtmlHelper Html
		{
			get
			{
				return _htmlHelper;
			}
		}

		public SlamCmsContext Context
		{
			get
			{
				return _slamCmsContext.Value;
			}
		}

		public SitemapHtmlHelper Sitemap(string sitemapId)
		{
			return new SitemapHtmlHelper(this, Context.Data.GetSitemap(sitemapId));
		}

		public SitemapHtmlHelper Sitemap<T>(string sitemapId = null) where T : Sitemap
		{
			return new SitemapHtmlHelper(this, Context.Data.GetSitemap<T>(sitemapId));
		}

		public IHtmlString RenderGoogleAnalytics()
		{
			var googleAnalyticsCode = ConfigurationHelpers.GetEnvironmentSetting(Constants.Settings.GoogleAnalyticsCode, string.Empty);

			if (googleAnalyticsCode.IsNullOrWhiteSpace())
				return null;

			var html = @"<script type=""text/javascript"">
						 var _gaq = _gaq || [];
						 _gaq.push(['_setAccount', '{code}']);
						 _gaq.push(['_trackPageview']);
						 (function() {
						   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
						   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
						   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
						 })();
						</script>".Replace("{code}", googleAnalyticsCode);
			return html.ToHtml();
		}

		public IHtmlString RenderTracking()
		{
			if (!ConfigurationHelpers.GetEnvironmentSetting(Constants.Settings.TrackingEnabled, true))
				return null;

			var html = @"<script type=""text/javascript"">
							function setTrackingType(value) {
								document.cookie = '_tt='+escape(value)+';path=/';
							}
							$().ready(function () {
								document.cookie = '_tt=;expires=Thu, 01-Jan-70 00:00:01 GMT;path=/';
								$('a').click(function () {
									var $this = $(this);
									var how = 'generic';
									var trackingContainerType = $this.parents('.tracking-container:first').data('trackingtype');
									if (trackingContainerType != null)
										how = trackingContainerType;
									var type = $this.data('trackingtype');
									if (type != null)
										how = type;
									setTrackingType(how);
								});
							});
						</script>";

			return html.ToHtml();
		}

		public IHtmlString RenderIncludes()
		{
			var includeRenderer = new IncludeRenderer();
			includeRenderer.Add(RenderGoogleAnalytics());
			includeRenderer.Add(RenderTracking());
			includeRenderer.Add(MiniProfiler.RenderIncludes());
			_htmlHelper.RenderAction("Index", "Bar", new { area = "_admin" });
			return includeRenderer;
		}
	}
}
